<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader
      :list="list"
      @addTask="addTask"
      @quanxuan="quanxuan"
    ></TodoHeader>
    <TodoMain :list="showlist" @change1="change1" @del="del"></TodoMain>
    <TodoFooter
      :list="list"
      :type="type"
      @change2="change2"
      @clear="clear"
    ></TodoFooter>
  </section>
</template>

<script>
// 1.0 样式引入
import './styles/base.css'
import './styles/index.css'

import TodoHeader from './components/TodoHeader'
import TodoMain from './components/TodoMain'
import TodoFooter from './components/TodoFooter'

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list:JSON.parse(localStorage.getItem('list')) ||[],
      type: 'all',
    }
  },
  methods: {
    change1(id) {
      this.list.find((item) => item.id == id).isDone = !this.list.find(
        (item) => item.id == id
      ).isDone
    },
    change2(a) {
      this.type = a
    },
    addTask(task) {
      this.list.push({
        id: this.list.length ? this.list.at(-1).id + 1 : 100,
        name: task,
        isDone: false,
      })
    },
    quanxuan(v) {
      this.list.forEach((item) => (item.isDone = v))
    },
    del(id) {
      this.list = this.list.filter((item) => item.id !== id)
    },
    clear() {
      this.list = this.list.filter((item) => !item.isDone)
    },
  },
  computed: {
    showlist() {
      if (this.type == 'yes') {
        return this.list.filter((item) => item.isDone)
      }
      if (this.type == 'no') {
        return this.list.filter((item) => !item.isDone)
      } else {
        return this.list
      }
    },
  },
  watch:{
    list:{
      deep: true,
      handler(newval) {
        localStorage.setItem('list', JSON.stringify(newval))
      }
    }
  }
}
</script>
